package com.machine.filling.function.init.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.machine.filling.MyApp;
import com.machine.filling.R;
import com.machine.filling.base.baseMvp.BaseFragment;
import com.machine.filling.function.init.INextFragment;
import com.machine.filling.function.init.InputOilStationNum.InputOilStationNumFragment;
import com.machine.filling.function.init.OilGunList.OilGunListFragment;
import com.machine.filling.function.main.activity.MainFuelActivity;
import com.machine.filling.function.set.orderList.OrderListFragment;
import com.machine.filling.util.CustomToast;

/**
 * Created by Administrator on 2018/1/11 0011.
 */

@SuppressLint("ValidFragment")
public class LoginFragment extends BaseFragment<LoginContract.Presenter> implements LoginContract.View, View.OnClickListener {

    private EditText et_name;
    private EditText et_psw;
    private Button bt_login;
    private Button bt_cancle;
    private TextView tv_devicenum;
    private boolean isFillerLogin;//是否是加油员登录模式, 此模式,只有在设置界面,点击"订单查询"时调用

    /**
     * @param mIntent
     */
    public LoginFragment(Intent mIntent) {
        super(mIntent);
    }

    @Override
    public View initViews() {

        View view = LayoutInflater.from(mActivity).inflate(R.layout.fg_login, null);
        et_name = (EditText) view.findViewById(R.id.et_name);
        tv_devicenum = (TextView) view.findViewById(R.id.tv_devicenum);
        et_psw = (EditText) view.findViewById(R.id.et_psw);
        bt_login = (Button) view.findViewById(R.id.bt_login);
        bt_cancle = (Button) view.findViewById(R.id.bt_cancle);
        bt_login.setOnClickListener(this);
        bt_cancle.setOnClickListener(this);
        return view;
    }

    @Override
    public void initDatas() {
        tv_devicenum.setText(getDeviceNum());
        boolean isCanCancle = fragmentIntent.getBooleanExtra("isCanCancle", false);
        isFillerLogin = fragmentIntent.getBooleanExtra("isFillerLogin", false);

        bt_cancle.setVisibility(isCanCancle ? View.VISIBLE : View.GONE);
        mPresenter = new LoginPresenter(mContext, this);
    }

    @Override
    public void showBindedResult() {
        CustomToast.showToast(mContext, "登录成功");
        INextFragment.replaceNext(new Intent(), OilGunListFragment.class.getName());
    }

    /**
     * 未绑定： 跳转查询油站信息
     */
    @Override
    public void showUnBindedResult() {
        INextFragment.replaceNext(new Intent(), InputOilStationNumFragment.class.getName());
    }

    private INextFragment INextFragment;

    public void setINextFragment(INextFragment INextFragment) {
        this.INextFragment = INextFragment;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_login:
                if (isFillerLogin) {
                    mPresenter.fillerLogin();
                } else {
                    mPresenter.login();
                }
                break;
            case R.id.bt_cancle:
                INextFragment.replaceNext(new Intent(), MainFuelActivity.class.getName());
                break;
        }
    }

    @Override
    public String getDeviceNum() {
        return Settings.System.getString(MyApp.getInstance().getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    @Override
    public String getLoginName() {
        return et_name.getText().toString().trim();
    }

    @Override
    public String getLoginPsw() {
        return et_psw.getText().toString().trim();
    }

    @Override
    public void showFillerLoginSuccess() {
        INextFragment.replaceNext(new Intent(), OrderListFragment.class.getName());
    }

}
